package com.xckj.ibox.presenter;


import androidx.annotation.NonNull;

import com.xckj.ibox.bean.BaseBean;
import com.xckj.ibox.framework.net.RetrofitManager;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.iview.ISendCodeView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 发送验证码
 * @Date: 2023/6/12 16:39
 */
public class SendCodePresenter {
    private final ISendCodeView sendCodeView;

    public SendCodePresenter(ISendCodeView sendCodeView) {
        this.sendCodeView = sendCodeView;
    }

    public void sendCode(String phone) {
        sendCodeView.showLoading();
        Call<BaseBean<String>> call = RetrofitManager.getInstance().getService().sendCode(phone, "7");
        call.enqueue(new Callback<BaseBean<String>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<String>> call, @NonNull Response<BaseBean<String>> response) {
                if (response.isSuccessful()) {
                    BaseBean<String> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        sendCodeView.sendSuccess();
                    } else {
                        sendCodeView.sendFailed(baseBean.msg);
                    }
                } else {
                    sendCodeView.sendFailed("请求失败");
                }
                sendCodeView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<String>> call, @NonNull Throwable t) {
                t.printStackTrace();
                sendCodeView.sendFailed("请求失败");
                sendCodeView.hideDialog();
            }
        });
    }
}
